export function createElement(type, attributes, ...children){
  let element
  if(typeof type === 'string')
    // element= document.createElement(type)
    element = new ElementWrapper(type)
  else
    element = new type
  for(let name in attributes){
    element.setAttribute(name, attributes[name])
  }
  for(let child of children){
    // 遇到文本需要将其转换成文本节点
    if(typeof child === 'string'){
      // child = document.createTextNode(child)
      child = new TextWrapper(child)
    }
    element.appendChild(child)
  }
  return element
}

export class Component {
  constructor(type){
    // this.root = this.render(type)
  }

  setAttribute(name, value){
    this.root.setAttribute(name, value)
  }

  appendChild(child){
    child.mountTo(this.root)
  }

  mountTo(parent){
    parent.appendChild(this.root)
  }
}

class ElementWrapper extends Component{
  constructor(type){
    super()
  }

  render(type){
    return document.createElement(type)
  }
}

class TextWrapper extends Component{
  constructor(content){
    super()
  }

  render(content){
    return document.createTextNode(content)
  }
}